package academic.emailClient.control.listeners;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import academic.emailClient.control.MessageMapper;
import academic.emailClient.model.EmailClientModel;
import academic.emailClient.model.entities.Filter;
import academic.emailClient.model.entities.FilterBuilder;
import academic.emailClient.view.EmailClientViewInterface;

public class ExecuteFilterListener implements ActionListener {

	private EmailClientViewInterface view;
	private EmailClientModel model;



	public ExecuteFilterListener(EmailClientViewInterface view, EmailClientModel model) {
		super();
		this.view = view;
		this.model = model;
	}

	/**
	 * Handle Execute Filter event
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		FilterBuilder filter = new FilterBuilder();
		Filter newFilter = filter.withName(view.getFilterName())
			  					 .withDestinationFolderPath(view.getFilterPath())
			  					 .withQuery(view.takingTheValues())
			  					 .build();

		model.createFilter(newFilter);
		model.executeFilter(newFilter);
		view.updateMailBoxList(MessageMapper.mapToMessageViewList(model.getIboxMessages()));
	}
}
